package slm.games.tris;

import android.util.Log;

public class Game {
	
	private Player p1 = null;
	private Player p2 = null;
	
	static final int BLANK= 0;
    static final int CROSS= 1;
    static final int CIRCLE= 2;
    
    static final int MODE_2_PLAYERS_LOCAL = 0;
    static final int MODE_2_PLAYERS_BT = 1;
    
	private int[][] winner_combs = {{0,1,2}, {3,4,5},{6,7,8}, 
			                		 {0,3,6}, {1,4,7}, {2,5,8},
			                         {0,4,8}, {2,4,6}};
	
	private int [] table_status = new  int [9];
	
	private boolean p1_locked = false;
	private boolean first_move = true;
	
	private final String TAG = "GameLogic";
	 
	public Game(Player p1, Player p2, boolean first_move) throws Exception
	{
		if (p1==null || p2==null)
			throw new NullPointerException("Players must not be null!");
		
		this.p1 = p1;
		this.p2 = p2;
		this.first_move = first_move;
		this.p1_locked  = !first_move;
		
		 
		if (this.p1.getPlayerSymbol()==this.p2.getPlayerSymbol())
			throw new Exception("Both players are using the same symbol " + this.p1.getPlayerSymbol());
	}
	
	public void reset()
	{
		this.start();
	}
	
	public void start()
	{
		this.init_table();
		this.p1.setWinner_comb_index(-1);
		this.p2.setWinner_comb_index(-1);
		this.p1_locked = ! this.first_move;
	}
	
	
	private void init_table() {
	    	for (int i=0;i< table_status.length;i++)
	    		table_status[i] = Game.BLANK;
	    }
	 
	public Player getLocalPlayer()
	{
		return this.p1;
	}
	
	public Player getRemotePlayer()
	{
		return this.p2;
	}
	
	public Player check_for_winner()
	{
		for (int i=0; i< winner_combs.length;i++)
		{
			if (table_status[winner_combs[i][0]]==this.p1.getPlayerSymbol() &&
					table_status[winner_combs[i][1]]==this.p1.getPlayerSymbol() &&
							table_status[winner_combs[i][2]]==this.p1.getPlayerSymbol())
				{
				  this.p1.setWinner_comb_index(i);
				  Log.d(TAG,"Found winner P1: " +  this.p1.getNickname());
				  return this.p1;
				}
			else
				if (table_status[winner_combs[i][0]]==this.p2.getPlayerSymbol() &&
						table_status[winner_combs[i][1]]==this.p2.getPlayerSymbol() &&
								table_status[winner_combs[i][2]]==this.p2.getPlayerSymbol())
						{
						  this.p2.setWinner_comb_index(i);
						  Log.d(TAG,"Found winner P2: " +  this.p2.getNickname());
						  return this.p2;
						}
			
		}
		Log.d(TAG,"NO WINNER");
		return null; // None winner
	}
	
	public boolean p1_move(int pos)
	{
 		if (p1_locked)
 			return false;
    	else
		{
		boolean move_ok = this.update_table_pos(pos, this.p1.getPlayerSymbol());
		if (move_ok)
			p1_locked = true;
		return move_ok;
		}
	}
	
	public boolean p2_move(int pos)
	{
 		if (!p1_locked)
 			return false;
 		else
		{
			boolean move_ok = this.update_table_pos(pos, this.p2.getPlayerSymbol());
			if (move_ok)
				p1_locked = false;
			return move_ok;
			}
	}
	
	public Player doNextMove(int pos)
	{
		boolean moved_ok = p1_move(pos);
		if (!moved_ok)
		{
			moved_ok = p2_move(pos);
			if (moved_ok)
				return this.p2;
			else return null;
		}
		else
		return this.p1;
		
	}
	private boolean is_cell_blank(int pos)
    {
    	return ( ( pos>=0) && (pos < this.table_status.length) && (this.table_status[pos]== Game.BLANK) );		 
    }
    
    private boolean update_table_pos(int pos, int val)
    {
    	if (val== Game.BLANK || (is_cell_blank(pos) && val>0 && val<=2))
    	{
    		table_status[pos] = val; 
    		Log.d(TAG, "UPDATED POS " + String.valueOf(pos) + " TO " + String.valueOf(val));
    		return true;
    	}
    	else return false;
    }
	 

}
